home *** CD-ROM | disk | FTP | other *** search
/ SVM Mac CD-ROM 17 / SVM Mac CD-ROM - No 17.iso / Accès direct / Trésors du domaine public / Programmation / launch-creator / launcher.cc < prev    next >
Text File  |  1996-06-08  |  4KB  |  125 lines

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *        This is a simple program that, given a file name or a FSSpec,
  5.  * launches an application-creator and has it handle the file. The net
  6.  * result is exactly the same as if the user had double-clicked on the file.
  7.  *
  8.  * Synopsis
  9.  *        void open_selection(const FSSpec& file_spec)
  10.  *        void open_selection(const char * full_path_name)
  11.  * Where the 'full_path_name' tells the full path name of the file that
  12.  * should be "double-clicked". Though, a relative path name would do,
  13.  * too. The 'file_spec' tells the file specification (volume refno,
  14.  * directory ID and the file name) of the file to "double-click" on.
  15.  *
  16.  * The present program achieves the magic by sending an 'Open Selection'
  17.  * event to the Finder. It is significantly based on the FinderEvents
  18.  * stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
  19.  *
  20.  * OpenSelection event requires two parameters:
  21.  *    - alias of the Folder the file resides
  22.  *    - list containing an alias of the file(s) to "double-click" on
  23.  *
  24.  *
  25.  ***********************************************************************
  26.  */
  27.  
  28. /* MacHeaders Included */
  29. #include "mymenv.h"
  30. #include <string.h>
  31. #include <AppleEvents.h>
  32. #include <Aliases.h>
  33.  
  34.  
  35.                             // Create an 'open selection' event to be sent to Finder
  36. static AppleEvent create_event_for_finder(void)
  37. {
  38.     const AEEventClass     kAEFinderEvents = 'FNDR';        // Finder event
  39.     const AEEventID     kOpenSelection = 'sope';        // OpenSelection event
  40.     const OSType         kFinderSignature = 'MACS';        // Our destination
  41.     AppleEvent             the_event;
  42.     AEAddressDesc         finder_id;
  43.     
  44.     do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
  45.                        &finder_id) );
  46.  
  47.     do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
  48.                             kAutoGenerateReturnID,
  49.                             kAnyTransactionID,
  50.                             &the_event) );
  51.  
  52.     do_well( AEDisposeDesc(&finder_id) );                    // the_event is created, finder_id can
  53.                                                               // be disposed of now
  54.       
  55.     return the_event;
  56. }
  57.  
  58.                                     // Add the alias of file or path name to the event
  59.                                     // Only the path field is considered
  60. static void add_path_name(AppleEvent * the_event_ptr, const FSSpec& file_spec)
  61. {
  62.     AliasHandle alias;
  63.     
  64.     FSSpec dir_spec;
  65.     do_well( FSMakeFSSpec(file_spec.vRefNum,file_spec.parID,"\p",&dir_spec) );
  66.     do_well( NewAliasMinimal(&dir_spec,&alias) );
  67.     HLock((char **)alias);
  68.     do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
  69.                          (**alias).aliasSize) );
  70.  
  71.     HUnlock((char **)alias);
  72.     DisposHandle((char **)alias);
  73. }
  74.  
  75.  
  76.                                     // Create a "selection" item in the event from the
  77.                                     // full file name specified 
  78. static void add_selection(AppleEvent * the_event_ptr, const FSSpec& file_spec)
  79. {
  80.     AliasHandle alias;
  81.     AEDescList selection_list;
  82.     
  83.     do_well( NewAliasMinimal(&file_spec,&alias) );
  84.  
  85.     do_well( AECreateList(nil, 0, FALSE, &selection_list) );
  86.     HLock((char **)alias);
  87.     do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias), 
  88.                    (**alias).aliasSize) );
  89.     HUnlock((char **)alias);
  90.     DisposHandle((char **)alias);
  91.  
  92.     do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
  93.     do_well( AEDeleteItem(&selection_list, 1) );
  94.     assert( AEDisposeDesc(&selection_list) == noErr );
  95. }
  96.  
  97.  
  98. //-------------------------------------------------------------------------
  99. //                            Routing module
  100. //
  101.  
  102. void open_selection(const FSSpec& file_spec)
  103.     AppleEvent the_event, reply;
  104.     
  105.     the_event = create_event_for_finder();
  106.     add_path_name(&the_event,file_spec);
  107.     add_selection(&the_event,file_spec);
  108.     
  109.     do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract, 
  110.                  kAENormalPriority, kAEDefaultTimeout, nil, nil) );
  111.  
  112.     do_well( AEDisposeDesc(&the_event) );
  113.     do_well( AEDisposeDesc(&reply) );
  114. }
  115.  
  116. void open_selection(const char * full_path_name)
  117. {
  118.     FSSpec file_spec;
  119.     
  120.     do_well( FSMakeFSSpec(0,0,(Pstr)full_path_name,&file_spec) );
  121.     open_selection(file_spec);
  122. }
  123.  
  124.